package com.example.administrator.social.adapter;

import android.content.Context;
import android.view.View;

import com.bumptech.glide.Glide;
import com.example.administrator.social.R;
import com.example.administrator.social.model.CharImage;
import com.example.administrator.social.model.CharText;
import com.example.administrator.social.model.CharVideo;
import com.example.administrator.social.model.CharVoice;
import com.example.administrator.social.util.HttpUtils;

import java.util.List;

/**
 * Created by Administrator on 2018/9/11 0011.
 */
public class CharRecyclerAdapter<T> extends CharBaseAdapter<T>{


    public CharRecyclerAdapter(Context context, List<T> data) {
        super(context, data);
    }

    @Override
    public void onBindViewHolder(CharBaseAdapter.ViewHolder holder, int position) {
        Object obj = mData.get(position);
        if (obj instanceof CharText){
            bindCharTextData(holder,position, (CharText) obj);
        }else if(obj instanceof CharImage){
            bindCharImageData((CharImage) obj);
        }else if(obj instanceof CharVoice){
            bindCharVoiceData((CharVoice) obj);
        }else if(obj instanceof CharVideo){
            bindCharVideoData((CharVideo) obj);
        }
    }
    @Override
    public int getContentType(int position) {
        T data = mData.get(position);
        if (data instanceof CharText){
            return  ((CharText)data).getContentType();
        }else if(data instanceof CharImage){
            return  ((CharImage)data).getContentType();
        }else if(data instanceof CharVoice){
            return  ((CharVoice)data).getContentType();
        } else if(data instanceof CharVideo){
            return  ((CharVideo)data).getContentType();
        }
        return  ((CharText)data).getContentType();
    }



    private void bindCharTextData(ViewHolder holder, int position,CharText obj) {
        switch (obj.getCharType()){
            case TYPE_SEND :
                holder.mCharTextLeftLayout.setVisibility(View.GONE);
                holder.mCharTextRightLayout.setVisibility(View.VISIBLE);
                if (obj.getHeaderImage()==null||obj.getHeaderImage().equals("")){
                    Glide.with(mContext).load(HttpUtils.HTTP_URL+"img/defualt_header_icon.jpeg").into(holder.mCharRightHeaderImage);
                }else{
                    Glide.with(mContext).load(obj.getHeaderImage()).into(holder.mCharRightHeaderImage);
                }
                holder.mCharRightText.setText(obj.getContent());
                holder.mMessageSendErrorRight.setImageResource(R.drawable.message_send_error);

                //控制消息发送状态图标
                holder.mMessageSendErrorRight.setVisibility(View.GONE);
                holder.mCharTextProgressBarRight.setVisibility(View.GONE);
                holder.mMessageSendErrorRight.setVisibility(obj.isSendSuccess() == -1 ? View.VISIBLE : View.GONE);
                holder.mCharTextProgressBarRight.setVisibility(obj.isSendSuccess() == 0 ? View.VISIBLE : View.GONE);

                break;
            case TYPE_RECEIVED :
                holder.mCharTextLeftLayout.setVisibility(View.VISIBLE);
                holder.mCharTextRightLayout.setVisibility(View.GONE);

                if (obj.getHeaderImage()==null||obj.getHeaderImage().equals("")){
                    Glide.with(mContext).load(HttpUtils.HTTP_URL+"img/defualt_header_icon.jpeg").into(holder.mCharLeftHeaderImage);
                }else{
                    Glide.with(mContext).load(obj.getHeaderImage()).into(holder.mCharLeftHeaderImage);
                }
                holder.mCharLeftText.setText(obj.getContent());


                break;
        }
    }

    private void bindCharVideoData(CharVideo obj) {
    }

    private void bindCharVoiceData(CharVoice obj) {

    }

    private void bindCharImageData(CharImage obj) {
    }

}
